package com.eduManage.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eduManage.beans.Eleve;


 
public class EleveDAOImpl implements EleveDAO {
	
	 private static final Logger logger = LoggerFactory.getLogger(EleveDAOImpl.class); 
	
	 private SessionFactory sessionFactory;
     
	 public void setSessionFactory(SessionFactory sf){
	        this.sessionFactory = sf;
	    }

	@Override
	public void addEleve(Eleve eleve) {
		// TODO Auto-generated method stub
		
		 Session session = this.sessionFactory.getCurrentSession();
	        session.persist(eleve);
	        logger.info("Eleve saved successfully, Eleve Details="+eleve);
		
	}

	@Override
	public void updateEleve(Eleve eleve) {
		// TODO Auto-generated method stub
		
		 Session session = this.sessionFactory.getCurrentSession();
	        session.update(eleve);
	        logger.info("Eleve updated successfully, Eleve Details="+eleve);
		
	}

	@Override
	public List<Eleve> listEleves() {
		// TODO Auto-generated method stub
		
		 Session session = this.sessionFactory.getCurrentSession();
	        List<Eleve> elevesList = session.createQuery("from Eleve").list();
	        for(Eleve eleve : elevesList){
	            logger.info("Eleve List::"+eleve);
	        }
	        
	        return elevesList;
	}

	@Override
	public Eleve getEleveById(int id) {
		// TODO Auto-generated method stub
		
		Session session = this.sessionFactory.getCurrentSession();      
        Eleve eleve = (Eleve) session.load(Eleve.class, new Integer(id));
        logger.info("Eleve loaded successfully, Eleve details="+eleve);
        return eleve;
	}
	
	@Override
	public Eleve getEleveByMatricule(int matricule) {
		// TODO Auto-generated method stub
/*		Session session = this.sessionFactory.getCurrentSession();      
        Eleve eleve = (Eleve) session.load(Eleve.class, new Integer(id));
        logger.info("Eleve loaded successfully, Eleve details="+eleve);*/
        
        return null;
		
	}
	
	

	@Override
	public void removeEleve(int id) {
		// TODO Auto-generated method stub
		
		Session session = this.sessionFactory.getCurrentSession();
        Eleve eleve = (Eleve) session.load(Eleve.class, new Integer(id));
        if(null != eleve){
            session.delete(eleve);
        }
        logger.info("Eleve deleted successfully, Eleve details="+eleve);
    }


		

}
